<template>
  <div class="login">
    <el-form
      ref="ruleForm"
      :model="username"
      :rules="rulesForm"
      label-width="120"
      class="demo-ruleForm"
    >
      <img
        src="@/assets/logo2.jpg"
      />
      <el-form-item label="账号" prop="name">
        <el-input v-model="username.name" clearable></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="username.password" show-password clearable></el-input>
      </el-form-item>
      <el-button type="success" @click="toLogin">登录</el-button>
    </el-form>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, reactive, toRefs, unref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'
interface userName {
  name: string,
  password: string | number
}
export default defineComponent({
  setup () {
    // 表单ref
    const ruleForm = ref<any>()// 获取元素的ref属性
    // 引入路由
    const router = useRouter()
    // console.log('router', router)
    const formData = reactive({
      username: {
        name: 'admin',
        password: '123456'
      } as userName,
      // 表单验证
      rulesForm: {
        name: [
          {
            required: true,
            message: '请输入账号名称',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 5,
            message: '请输入3-5个字符长度',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 9,
            message: '请输入3-9个字符长度',
            trigger: 'blur'
          }
        ]
      },
      // 登录
      toLogin: () => {
        const volidForm = unref(ruleForm) // 表单元素的预校验
        volidForm.validate((volid: boolean) => {
          if (volid) {
            axios.post('/user/login', formData.username).then((res: any) => {
              // console.log(res)
              window.sessionStorage.setItem('token', res.data.token)
              ElMessage.success('验证成功')
              unref(ruleForm).resetFields()// 表单重置
              router.push({
                path: '/welcome',
                query: {
                  id: '123'
                }
              })
            }).catch((err: any) => console.log(err))
          }
        })
      }
    })
    return {
      ruleForm,
      ...toRefs(formData) // reactive中的数据直接导出有可能会丧失响应性，使用toRefs使数据重新获得相应性
    }
  }
})
</script>
<style lang="scss" scoped>
.login {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.demo-ruleForm {
  width: 400px;
  padding: 30px;
  border-radius: 20px;
  background-color: bisque;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .el-form-item {
    width: 100%;
  }
  .el-button {
    width: 30%;
  }
  img {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    transform: translateY(-80px);
  }
}
</style>
